package net.zigin.nhi.could.waste.service.impl;

import net.zigin.commons.util.AssertUtil;
import net.zigin.commons.util.ParamsMapUtil;
import net.zigin.commons.util.ReflectUtil;
import net.zigin.nhi.could.waste.aop.LoginAspect;
import net.zigin.nhi.could.waste.dao.*;
import net.zigin.nhi.could.waste.dto.WarnBaseDto;
import net.zigin.nhi.could.waste.model.*;
import net.zigin.nhi.could.waste.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import net.zigin.nhi.could.waste.service.WarnBaseService;

import java.util.Date;
import java.util.List;
import java.util.Map;

/*#################################################################*
# 说明:以下代码通过哲勤代码生成工具自动生成    
# 工具:GeneratorCode
# 作者:mgw1986                                                  
# 生成时间:2021-9-20 19:47:08                                               
##################################################################*/


/**
 * WarnBase服务实现类
 *
 * @author mgw1986
 * @version V1.0
 * @ClassName: WarnBaseServiceImpl
 * @date 2021-9-20 19:47:08
 */
@Service
public class WarnBaseServiceImpl extends AbstractServiceImpl<WarnBase, String> implements WarnBaseService {
    @Autowired
    private WarnBaseMapper warnBaseMapper;
    @Autowired
    private WasteBaseMapper wasteBaseMapper;
    @Autowired
    private WarnWeightMapper warnWeightMapper;
    @Autowired
    private SysUserHospitalMapper sysUserHospitalMapper;
    @Autowired
    private WasteBaseRecordMapper wasteBaseRecordMapper;

    @Override
    public BaseMapper<WarnBase, String> getMapper() {
        return warnBaseMapper;
    }

    @Override
    public void dealWith(WarnBaseDto warnBaseDto) {
        TokenInfoBo tokenInfo = LoginAspect.getTokenInfo();
        SysUserHospital userHospital = sysUserHospitalMapper.getBySysUserId(tokenInfo.getUserId());
        AssertUtil.isNull(warnBaseDto.getId(), "");
        AssertUtil.isNull(warnBaseDto.getRemark(), "");
        WarnBase warnBase = ReflectUtil.copyProperties(warnBaseDto, WarnBase.class);
        warnBase.setStatus("done");
        warnBase.setModifyUser(userHospital.getRealName());
        warnBase.setModifyTime(new Date());
        warnBaseMapper.updateByPrimaryKeySelective(warnBase);
    }

    @Override
    public WarnBaseVo getInfoById(String id) {
        WarnBase warnBase = warnBaseMapper.selectByPrimaryKey(id);
        WasteBase wasteBase = wasteBaseMapper.selectByPrimaryKey(warnBase.getWasteBaseId());
        WarnBaseVo warnBaseVo = ReflectUtil.copyProperties(warnBase, WarnBaseVo.class);
        WasteBaseVo wasteBaseVo = ReflectUtil.copyProperties(wasteBase, WasteBaseVo.class);
        warnBaseVo.setWasteBaseVo(wasteBaseVo);
        return warnBaseVo;
    }

    @Override
    public WarnCountVo situation() {
        // 近7日 正在（当日） 已处理
        TokenInfoBo tokenInfo = LoginAspect.getTokenInfo();
        String hospitalBaseId = sysUserHospitalMapper.getHospitalBaseIdBySysUserId(tokenInfo.getUserId());
        return warnBaseMapper.situation(hospitalBaseId);
    }

    @Override
    public List<WarnBaseVo> getList(WarnBaseDto warnBaseDto) {
        Map<String, Object> paramMap = getWarnBaseParam(warnBaseDto);
        return warnBaseMapper.getInfoPageByCondition(paramMap);
    }

    @Override
    public Integer getCount(WarnBaseDto warnBaseDto) {
        Map<String, Object> paramMap = getWarnBaseParam(warnBaseDto);
        return warnBaseMapper.getCountByCondition(paramMap);
    }

    @Override
    public String checkWeightRule(String step, String wasteClassifyId, Float oldWeight, Float newWeight) {
        // 获取规则
        TokenInfoBo tokenInfo = LoginAspect.getTokenInfo();
        String hospitalBaseId = sysUserHospitalMapper.getHospitalBaseIdBySysUserId(tokenInfo.getUserId());
        WarnWeight rule = warnWeightMapper.getRuleByWasteClassifyIdAndHospitalBaseId(wasteClassifyId, hospitalBaseId);
        // 判断是否符合红色
        Boolean isRead = checkWeight(rule.getYellowValue(), rule.getYellowUnit(), oldWeight, newWeight, rule.getYellowSymbol());
        if (isRead) {
            return step + "_weight_red";
        }
        // 判断是否符合黄色
        Boolean isYellow = checkWeight(rule.getRedValue(), rule.getRedUnit(), oldWeight, newWeight, rule.getRedSymbol());
        if (isYellow) {
            return step + "_weight_yellow";
        }
        return "";
    }

    @Override
    public WasteWarnVo getWarnTimeAndWeight(String wasteBaseId) {
        WasteWarnVo wasteWarnVo = new WasteWarnVo();
        List<WasteBaseRecord> wasteBaseRecords = wasteBaseRecordMapper.getListByWasteBaseId(wasteBaseId);
        for (WasteBaseRecord wasteBaseRecord : wasteBaseRecords) {
            if (wasteBaseRecord.getType().equals("do_collect")) {
                wasteWarnVo.setCollectWeight(wasteBaseRecord.getWeight());
                wasteWarnVo.setCollectTime(wasteBaseRecord.getTime());
            } else if (wasteBaseRecord.getType().equals("in_depot")) {
                wasteWarnVo.setInWeight(wasteBaseRecord.getWeight());
                wasteWarnVo.setInTime(wasteBaseRecord.getTime());
            } else if (wasteBaseRecord.getType().equals("out_depot")) {
                wasteWarnVo.setOutWeight(wasteBaseRecord.getWeight());
                wasteWarnVo.setOutTime(wasteBaseRecord.getTime());
            }
        }
        return wasteWarnVo;
    }

    private Boolean checkWeight(Float value, String unit, Float oldWeight, Float newWeight, String symbol) {
        float diff = Math.abs(newWeight - oldWeight);
        if (unit.equals("%")) {
            float ratio = (diff / oldWeight) * 100;
            if (symbol.equals(">=")) {
                return ratio >= value;
            } else {
                return ratio > value;
            }
        } else if (unit.equals("kg")) {
            if (symbol.equals(">=")) {
                return diff >= value;
            } else {
                return diff > value;
            }
        } else if (unit.equals("g")) {
            if (symbol.equals(">=")) {
                return diff * 1000 >= value;
            } else {
                return diff * 1000 > value;
            }
        }
        return false;
    }

    private Map<String, Object> getWarnBaseParam(WarnBaseDto warnBaseDto) {
        TokenInfoBo tokenInfo = LoginAspect.getTokenInfo();
        String hospitalBaseId = sysUserHospitalMapper.getHospitalBaseIdBySysUserId(tokenInfo.getUserId());
        return ParamsMapUtil.getInstance()
                .addParam("type", warnBaseDto.getType())
                .addParam("hospitalBaseId",hospitalBaseId)
                .addParam("hospitalDepartId", warnBaseDto.getHospitalDepartId())
                .addParam("warnTimeStart", warnBaseDto.getWarnTimeStart())
                .addParam("warnTimeEnd", warnBaseDto.getWarnTimeEnd())
                .addParam("pageIndex", warnBaseDto.getPageIndex())
                .addParam("pageSize", warnBaseDto.getPageSize());
    }


}